package server.business.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import server.business.pojo.BloodTake;
import server.business.service.BloodTakeService;
import server.business.mapper.BloodTakeMapper;

import java.util.List;

import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Galaxy
 * @description 针对表【blood_take】的数据库操作Service实现
 * @createDate 2022-11-08 12:46:24
 */
@Service("bloodTakeService")
public class BloodTakeServiceImpl extends ServiceImpl<BloodTakeMapper, BloodTake>
                implements BloodTakeService {
        /**
         * 装配数据库查询
         */
        @Autowired
        private BloodTakeMapper bloodTakeMapper;

        /**
         * 血液发血后添加一条待取血记录
         * 
         * @param bloodTake
         * @return
         */
        public void addRecord(BloodTake bloodTake) {
                bloodTakeMapper.insert(bloodTake);
        }

        /**
         * 分页查询
         *
         * @param currentPage
         * @param pageSize
         * @param bloodTake
         * @return
         */
        @Override
        @SuppressWarnings("")
        public IPage<BloodTake> getPage(int currentPage, int pageSize, BloodTake bloodTake) {
                LambdaQueryWrapper<BloodTake> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper
                                .eq(Strings.isNotBlank(bloodTake.getBloodId()), BloodTake::getBloodId,
                                                bloodTake.getBloodId())
                                .eq(Strings.isNotBlank(bloodTake.getTransfusionNumber()),
                                                BloodTake::getTransfusionNumber,
                                                bloodTake.getTransfusionNumber())
                                .eq(Strings.isNotBlank(bloodTake.getPatientIdcard()), BloodTake::getPatientIdcard,
                                                bloodTake.getPatientIdcard())
                                .eq(Strings.isNotBlank(bloodTake.getBloodTakerJobnum()),
                                                BloodTake::getBloodTakerJobnum, bloodTake.getBloodTakerJobnum())
                                .eq(Strings.isNotBlank(bloodTake.getBloodTakeNumber()),
                                                BloodTake::getBloodTakeNumber, bloodTake.getBloodTakeNumber())
                                .eq(Strings.isNotBlank(bloodTake.getDoctorJobnum()), BloodTake::getDoctorJobnum,
                                                bloodTake.getDoctorJobnum());
                IPage<BloodTake> page = new Page<>(currentPage, pageSize);
                bloodTakeMapper.selectPage(page, queryWrapper);
                return page;
        }

        /**
         * 检测取血表中是否已经有了记录
         * 
         * @param
         * @return
         */
        @Override
        public List<BloodTake> checkRecords(BloodTake bloodTake) {
                LambdaQueryWrapper<BloodTake> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper
                                .eq(Strings.isNotBlank(bloodTake.getPatientIdcard()),
                                                BloodTake::getPatientIdcard,
                                                bloodTake.getPatientIdcard())
                                .eq(Strings.isNotBlank(bloodTake.getTransfusionType()),
                                                BloodTake::getTransfusionType,
                                                bloodTake.getTransfusionType())
                                .eq(Strings.isNotBlank(bloodTake.getBloodComponent()),
                                                BloodTake::getBloodComponent,
                                                bloodTake.getBloodComponent())
                                .eq(bloodTake.getBloodCount() != null,
                                                BloodTake::getBloodCount,
                                                bloodTake.getBloodCount())
                                .eq(BloodTake::getState, "28"); // 28代表待取血
                List<BloodTake> list = bloodTakeMapper.selectList(queryWrapper);
                return list;
        }

        /**
         * 前来取血补足表格内其他信息
         * 
         * @param bloodTake
         * @return
         */
        @Override
        public void editData(BloodTake bloodTake) {
                LambdaUpdateWrapper<BloodTake> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(BloodTake::getBloodId, bloodTake.getBloodId());
                updateWrapper.set(BloodTake::getDoctorJobnum, bloodTake.getDoctorJobnum())
                                .set(BloodTake::getFetchTime, bloodTake.getFetchTime())
                                .set(BloodTake::getBloodTakerJobnum, bloodTake.getBloodTakerJobnum())
                                .set(BloodTake::getBloodTakeNumber, bloodTake.getBloodTakeNumber())
                                .set(BloodTake::getState, "27"); // "27"代表已取血
                bloodTakeMapper.update(null, updateWrapper);
        }
}
